using System;
using QFramework;
using UnityEngine;

namespace GGameFramework.Runtime
{
    public class AudioComponent : FrameworkComponent
    {
        public override FrameworkComponentPriority Priority => FrameworkComponentPriority.System;

        private AudioManager _audioManager;

        protected override void Awake()
        {
            base.Awake();

            AudioKit.Config.AudioLoaderPool = new ResKitAudioLoaderPool();
        }

        /// <summary>
        /// 游戏总音量
        /// </summary>
        public float TotalVolume
        {
            get { return AudioKit.Settings.TotalVolume.Value; }
            set { AudioKit.Settings.TotalVolume.Value = value; }
        }

        /// <summary>
        /// 背景音量
        /// </summary>
        public float BGMVolume
        {
            get { return AudioKit.Settings.MusicVolume.Value; }
            set { AudioKit.Settings.MusicVolume.Value = value; }
        }

        /// <summary>
        /// 音效音量
        /// </summary>
        public float SoundVolume
        {
            get { return AudioKit.Settings.SoundVolume.Value; }
            set { AudioKit.Settings.SoundVolume.Value = value; }
        }

        /// <summary>
        /// 设置背景音乐音量
        /// </summary>
        /// <param name="num"></param>
        public void SetTotalVolume(float volume)
        {
            if (volume < 0 || volume > 1)
            {
                Debug.LogWarning("BGM volume must be between 0 and 1");
                return;
            }

            TotalVolume = volume;

            SetBGMVolume(BGMVolume);
            SetSoundVolume(SoundVolume);
        }


        /// <summary>
        /// 设置背景音乐音量
        /// </summary>
        /// <param name="num"></param>
        public void SetBGMVolume(float volume)
        {
            if (volume < 0 || volume > 1)
            {
                Debug.LogWarning("BGM volume must be between 0 and 1");
                return;
            }

            BGMVolume = volume * TotalVolume;
        }

        /// <summary>
        /// 设置音效音量
        /// </summary>
        /// <param name="num"></param>
        public void SetSoundVolume(float volume)
        {
            if (volume < 0 || volume > 1)
            {
                Debug.LogWarning("BGM volume must be between 0 and 1");
                return;
            }

            SoundVolume = volume * TotalVolume;
        }


        /// <summary>
        /// 播放背景音乐
        /// </summary>
        /// <param name="musicName">音效名</param>
        /// <param name="loop">是否循环</param>
        /// <param name="onBeganCallback">开始播放回调</param>
        /// <param name="onEndCallback">播放结束回调</param>
        public void PlayMusic(string musicName, bool loop = true, Action onBeganCallback = null,
            Action onEndCallback = null)
        {
            AudioKit.PlayMusic(musicName, loop, onBeganCallback, onEndCallback);
        }

        /// <summary>
        /// 停止背景音乐,无法恢复
        /// </summary>
        public void StopMusic()
        {
            AudioKit.StopMusic();
        }

        /// <summary>
        /// 暂停背景音乐
        /// </summary>
        public void PauseMusic()
        {
            AudioKit.PauseMusic();
        }

        /// <summary>
        /// 恢复背景音乐
        /// </summary>
        public void ResumeMusic()
        {
            AudioKit.ResumeMusic();
        }

        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="soundName">音效名</param>
        /// <param name="loop">是否循环</param>
        /// <param name="callBack">播放结束回调</param>
        public AudioPlayer PlaySound(string soundName, bool loop = false, Action<AudioPlayer> callBack = null)
        {
            return AudioKit.PlaySound(soundName, loop, callBack);
        }


        /// <summary>
        /// 暂停所有音效
        /// </summary>
        public void StopAllSound()
        {
            AudioKit.StopAllSound();
        }

        /// <summary>
        /// 停止音效
        /// </summary>
        /// <param name="soundName">音效名</param>
        public void StopSound(string soundName)
        {
            AudioKit.StopSound(soundName);
        }

        /// <summary>
        /// 停止音效
        /// </summary>
        /// <param name="audioPlayer">音效播放组件</param>
        public void StopSound(AudioPlayer audioPlayer)
        {
            audioPlayer.Stop();
        }

        public void RegisterTotalVolumeChanged(Action<float> callback)
        {
            AudioKit.Settings.TotalVolume.Register(callback);
        }

        public void RegisterBGMVolumeChanged(Action<float> callback)
        {
            AudioKit.Settings.MusicVolume.Register(callback);
        }

        public void RegisterSoundVolumeChanged(Action<float> callback)
        {
            AudioKit.Settings.SoundVolume.Register(callback);
        }

        public void RegisterTotalVolumeChangedWithInit(Action<float> callback)
        {
            AudioKit.Settings.TotalVolume.RegisterWithInitValue(callback);
        }

        public void RegisterBGMVolumeChangedWithInit(Action<float> callback)
        {
            AudioKit.Settings.MusicVolume.RegisterWithInitValue(callback);
        }

        public void RegisterSoundVolumeChangedWithInit(Action<float> callback)
        {
            AudioKit.Settings.SoundVolume.RegisterWithInitValue(callback);
        }

        public void UnRegisterTotalVolumeChanged(Action<float> callback)
        {
            AudioKit.Settings.TotalVolume.UnRegister(callback);
        }

        public void UnRegisterMusicVolumeChanged(Action<float> callback)
        {
            AudioKit.Settings.MusicVolume.UnRegister(callback);
        }

        public void UnRegisterSoundVolumeChanged(Action<float> callback)
        {
            AudioKit.Settings.SoundVolume.UnRegister(callback);
        }
    }
}